// 导入db模块
const db = require('../config/db');

// 定义用户类
class User {
    // 注册(用户名，用户联系电话，密码，注册时间，邮箱（可选）)
    register(user_name, user_phone, user_pwd, user_register_time,user_email) {
        return new Promise((resolve, reject) => {
            // 查询sql语句
            let sql = `select user_phone from db_user where user_phone='${user_phone}'`;
            db.query(sql)
                .then(
                    (rows) => {
                        // 执行成功
                        if (rows.length > 0) {
                            // 表示账号已经存在
                            resolve({
                                code: 200,
                                msg: '该账号已经存在。',
                                user_phone
                            })
                        }
                        else {
                            // 添加账号信息
                            // 插入数据sql语句
                            let sql2 = `insert into db_user(user_name, user_phone, user_pwd,user_register_time,user_email) values('${user_name}','${user_phone}','${user_pwd}', '${user_register_time}','${user_email}')`;
                            // 返回添加数据的promise
                            return db.query(sql2)

                        }
                    },
                    () => {
                        // 执行失败
                        reject({
                            code: -1,
                            msg: '查询用户失败。'
                        })
                    }
                )
                .then(
                    (data) => {
                        resolve({
                            code: 200,
                            msg: '注册账号成功。',
                            user_phone,
                            user_register_time
                        })
                    },
                    (err) => {
                        reject({
                            code: -1,
                            msg: '注册账号失败。',
                            err
                        })
                    }
                )
        })
    }

    // 登录
    login(user_phone) {
        return new Promise((resolve, reject) => {
            // sql语句 (查询账号和密码和用户id  user_phone, user_pwd, user_id)
            let sql = `select user_phone, user_pwd, user_id , user_name from db_user where user_phone = '${user_phone}'`;
            db.query(sql)
                .then(
                    (rows) => {
                        // 处理用户信息
                        if (rows.length == 0) {
                            resolve({
                                code: 404,
                                msg: '该账号不存在。'
                            })
                        }
                        else {
                            resolve(rows)
                        }
                    },
                    (err) => {
                        reject({
                            code: -1,
                            msg: '查询用户信息失败。',
                            err
                        })
                    }
                )
        })
    }

    // 获取用户信息
    info(user_id) {
        return new Promise((resolve, reject) => {
            // 查询账号
            let sql = `select user_email, user_name , user_phone, user_register_time from db_user where user_id = '${user_id}'`;
            db.query(sql)
                .then(
                    (rows) => {
                        // console.log('y.2',rows);
                        if (rows.length == 1) {
                            resolve({
                                code: 200,
                                msg: '获取用户信息成功。',
                                data: rows
                            })
                        }
                        else {
                            resolve({
                                code: 404,
                                msg: '该账户不存在。'
                            })
                        }
                    },
                    (err) => {
                        reject({
                            code: -1,
                            msg: '查询账号失败。'
                        })
                    }
                )
        })
    }

    // 删除账号
    remove(user_phone, user_id) {
        return new Promise((resolve, reject) => {
            // 查询账号
            let sql = `select user_phone from db_user where user_phone = '${user_phone}' and user_id='${user_id}'`;
            db.query(sql)
                .then(
                    (rows) => {
                        if (rows.length == 1) {
                            // 删除数据表的sql语句
                            let sql2 = `delete from db_user where user_id = '${user_id}'`
                            db.query(sql2).then(
                                (result) => {
                                    if (result.affectedRows === 1) {
                                        resolve({
                                            code: 200,
                                            msg: '删除用户账号成功。',
                                        })
                                    }
                                    else {
                                        resolve({
                                            code: -1,
                                            msg: '删除用户账号失败。',
                                        })
                                    }
                                },
                                err => {
                                    reject(err)
                                }
                            )
                        }
                        else {
                            resolve({
                                code: 404,
                                msg: '该账户不存在。'
                            })
                        }
                    },
                    (err) => {
                        reject({
                            code: -1,
                            msg: '查询账号失败。'
                        })
                    }
                )
        })
    }

    // 获取所有用户数量
    total() {
        return new Promise((resolve, reject) => {
            // 统计db_message这个表格的内容数量
            let sql = `select count(*) as total from db_user`
            db.query(sql)
                .then(
                    result => {
                        resolve({
                            code: 200,
                            msg: `统计用户数量成功。`,
                            result
                        })
                    },
                    err => {
                        reject({
                            code: -1,
                            msg: '统计数据失败。'
                        })
                    }
                )
        })
    }

    // 修改用户名
    modify_name(user_id, user_name) {
        return new Promise((resolve, reject) => {
            let sql = `select user_id from db_user where user_id = '${user_id}'`;
            db.query(sql)
                .then(
                    (rows) => {
                        if (rows.length == 1) {
                            // 修改用户资料
                            let sql2 = `update db_user set user_name= '${user_name}' where  user_id= '${user_id}'`;

                            db.query(sql2)
                                .then(
                                    (result) => {
                                        // 判断是否修改成功
                                        if (result.affectedRows == 1) {
                                            resolve({
                                                code: 200,
                                                msg: '修改用户名成功。',
                                                user_name
                                            })
                                        }
                                        else {
                                            resolve({
                                                code: -1,
                                                msg: '修改用户名失败。'
                                            })
                                        }

                                    },
                                    (err) => {
                                        reject({
                                            code: -1,
                                            msg: '服务端发生异常。~~~',
                                            err
                                        })
                                    }
                                )
                        }
                        else {
                            resolve({
                                code: 404,
                                msg: '该用户不存在。'
                            })
                        }
                    },
                    (err) => {
                        reject({
                            code: -1,
                            msg: '服务端发生异常。111',
                            err
                        })
                    }
                )
        })
    }

}
// 用户类实例挂到系统对象上
module.exports = new User();